package edu.biwu.mapper;

import edu.biwu.po.Users;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UsersMapper {
    Users selectUserWithProfile(int id);

    @Select("SELECT * FROM mybatis02.users")
    List<Users> findAll();

    @Update("UPDATE mybatis02.users(name) VALUES(#{name}) WHERE id = #{id}")
    void updateUsers(Users users);

    @Insert("INSERT INTO mybatis02.users SET name = #{name}")
    void insertUsers(String name);

    @Delete("DELETE FROM mybatis02.users WHERE id = #{id} ")
    void deleteUsers(int id);

    @Select("SELECT * FROM mybatis02.users WHERE id = #{id}"
    )
    Users findById(int id);

    //用注解实现一个可以根据用户的不同条件动态查询的 UserMapper 接口。
    @Select({
            "<script>",
            "SELECT * FROM mybatis02.users",
            "<where>",
            "<if test=name!=null and name!=''",
            "AND name = #{name}",
            "<if/>",
            "<if test=email!=null and email!=''",
            "AND email = #{email}",
            "<if/>",
            "<where/>",
            "<script/>"
    })
    Users findByCondition(Users users);
}
